package com.example.gulimall.seckill.scheduled;

import com.example.gulimall.seckill.service.SecKillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


// 定时上架 每天晚上某个时间上架未来几天的文件
@Service
public class SeckillSkuScheduled {


    @Autowired
    SecKillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    private final String upload_lock= "seckill:upload:lock";

    // TODO 幂等性处理
    @Scheduled(cron = "0 * * * * ?")
    public void uploadSeckillSkuLatest3Days(){

        // 重复上架通过redis来保证分布式锁的机制
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLatest3Days();
        }finally {
            lock.unlock();
        }
    }
}
